package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.SysEnumData;
import com.ruoyi.system.service.SysEnumService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author BaiBan
 * @since 2023/2/12
 */
@Slf4j
@Service
public class SysEnumServiceImpl implements SysEnumService {

    private static final String packageName = "com.ruoyi.common.enums.";

    @Override
    public List<SysEnumData> get(String enumName) {
        String className = packageName + enumName;

        try {
            Class<?> objClass = Class.forName(className);
            Method codeMethod = objClass.getMethod("getCode");
            Method labelMethod = objClass.getMethod("getLabel");
            List<SysEnumData> enums = new ArrayList<>();
            for (Object obj : objClass.getEnumConstants()) {
                SysEnumData po = new SysEnumData();
                po.setCode(codeMethod.invoke(obj, null).toString());
                po.setLabel(labelMethod.invoke(obj, null).toString());
                enums.add(po);
            }
            return enums;
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException |
                 InvocationTargetException e) {
            log.info("枚举异常{}", enumName);
        }
        return null;
    }

    @Override
    public List<SysEnumData> list() {
        return null;
    }
}
